package com.bdqn.zhao.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.bdqn.zhao.config.Constants;
import com.bdqn.zhao.pojo.Menu;
import com.bdqn.zhao.pojo.R;
import com.bdqn.zhao.pojo.SysUser;
import com.bdqn.zhao.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@RestController
@RequestMapping("system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService menuService;


    /**
     * 查询菜单权限 查询指定id的菜单 并且会多查询一列:该父亲的名称
     */
    @GetMapping("get/{menuId}")
    public Menu get(@PathVariable("menuId") Long menuId) {
        return menuService.selectMenuById(menuId);
    }

    /**
     * 根据用户id查询权限
     */
    @GetMapping("perms/{userId}")
    public Set<String> perms(@PathVariable("userId") Long userId) {
        return menuService.selectPermsByUserId(userId);
    }

    /**
     * 查询菜单权限
     */
    @GetMapping("user")
    public List<Menu> user(HttpSession session) {
        //用户的登录信息
        SysUser dengLuUser = (SysUser) session.getAttribute("currentUser");
        if (dengLuUser == null) {
            SysUser sysUser = new SysUser();
            sysUser.setUserId(1);
            return menuService.selectMenusByUser(sysUser);
        }
        return menuService.selectMenusByUser(dengLuUser);
    }

    /**
     * 根据角色编号查询菜单编号（用于勾选）
     *
     * @param roleId
     * @return
     * @author zmr
     */
    @GetMapping("role/{roleId}")
    public List<Menu> role(@PathVariable("roleId") Long roleId) {
        if (null == roleId || roleId <= 0) {
            return null;
        }
        return menuService.selectMenuIdsByRoleId(roleId);
    }

    /**
     * 查询菜单权限列表
     */
    @GetMapping("list")
    public R list(Menu menu) {
        List<Menu> sysMenus = menuService.selectMenuList(menu);
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("rows", sysMenus);
        m.put("pageNum", 1);
        m.put("total", sysMenus.size());
        return R.ok(m);
    }


    /**
     * 查询所有菜单权限列表
     */
    @GetMapping("all")
    public R all(Menu menu) {
        List<Menu> sysMenus = menuService.selectMenuList(menu);
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("rows", sysMenus);
        m.put("pageNum", 1);
        m.put("total", sysMenus.size());
        return R.ok(m);
    }

    /**
     * 新增保存菜单权限
     */
    @PostMapping("save")
    public R addSave(Menu menu, HttpSession session) {
        //设置园区id
        String parkId = (String) session.getAttribute(Constants.PARK_ID);
        if (StringUtils.isNotBlank(parkId)) {
            menu.setParkId(Integer.valueOf(parkId));
        } else {
            menu.setParkId(null);
        }
        menu.setCreateBy(getLoginName());
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改保存菜单权限
     */
    @PostMapping("update")
    public R editSave(Menu menu, HttpSession session) {
        String parkId = (String) session.getAttribute(Constants.PARK_ID);
        if (StringUtils.isNotBlank(parkId)) {
            menu.setParkId(Integer.valueOf(parkId));
        } else {
            menu.setParkId(null);
        }

        //获取登陆的名字
        menu.setUpdateBy(getLoginName());
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单权限
     */
    @PostMapping("remove/{menuId}")
    public R remove(@PathVariable("menuId") Long menuId) {
        return toAjax(menuService.deleteMenuById(menuId));
    }
}



















